//
//  YFFirstLoginViewController.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/20.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFFirstLoginViewController.h"
#import "YFWelcomeDeviceVC.h"
#import "YFUpheadIocnVC.h"
#import "YFRacquetController.h"
#import "YFPersonDataVC.h"
#import "YFMMDrawerStateManager.h"
#import "JCTabBarViewController.h"

@interface YFFirstLoginViewController ()
@property (weak, nonatomic) UICollectionViewFlowLayout *layout;
/**
 *  控制器缓存
 */
@property (nonatomic, strong) NSArray *controllerCache;
@property (nonatomic, strong) UserInfoItem *cacheUserInfoItem;

@end

@implementation YFFirstLoginViewController

static NSString * const reuseIdentifier = @"FirstLoginCell";

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
    if (layout == nil) {
        layout = [[UICollectionViewFlowLayout alloc] init];
    }
    if (self = [super initWithCollectionViewLayout:layout]) {
        self.layout = (UICollectionViewFlowLayout *)layout;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cacheUserInfoItem = [[UserInfoItem alloc] init];
    [[User getUseInfo] copyValuesTo:self.cacheUserInfoItem];
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.yfBaseDelegate = self;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

// 返回按钮
- (void)customBaseBackNavBarItemAction:(UIBarButtonItem *)backItem {
    [self nextOrPrevious:NO];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self setupCollectionView];
}

- (void)setupCollectionView {
    self.collectionView.backgroundColor = kSeparationLineColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.layout.itemSize = self.collectionView.bounds.size;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.minimumLineSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.scrollEnabled = NO;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.controllerCache.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *vc = [self controllerWithIndexpath:indexPath];
    [cell.contentView addSubview:vc.view];
    // 设置frame
    vc.view.frame = cell.bounds;
    if (![self.childViewControllers containsObject:vc]) {
        [self addChildViewController:vc];
    }
    return cell;
}

- (UIViewController *)controllerWithIndexpath:(NSIndexPath *)indexPath {
    if (self.controllerCache.count == 0 || indexPath.row >= self.controllerCache.count) {
        return nil;
    }
    return [self.controllerCache objectAtIndex:indexPath.row];
}

- (NSArray *)controllerCache {
    if (_controllerCache == nil) {
        YFUpheadIocnVC *uploadIconVc = [[YFUpheadIocnVC alloc] initWithNibName:@"YFUpheadIocnVC" bundle:nil];
 
        uploadIconVc.cacheUserInfoItem = self.cacheUserInfoItem;
        __weak typeof(self) weakSelf = self;
        [uploadIconVc setNextButtonClick:^{
            [weakSelf nextOrPrevious:YES];
        }];
        YFPersonDataVC *personVc = [[YFPersonDataVC alloc] init];
        personVc.isWecomePersonVc = YES;
        personVc.cacheUserInfoItem = self.cacheUserInfoItem;
        [personVc setNextButtonClick:^{
            [weakSelf nextOrPrevious:YES];
        }];
        YFRacquetController *racquetVc = [[YFRacquetController alloc] init];
        racquetVc.isWecomeRacquetVc = YES;
        racquetVc.cacheUserInfoItem = self.cacheUserInfoItem;
        [racquetVc setNextButtonClick:^{
            [weakSelf nextOrPrevious:YES];
        }];
        
        YFWelcomeDeviceVC *deviceVc = [[YFWelcomeDeviceVC alloc] initWithNibName:@"YFWelcomeDeviceVC" bundle:nil];
        
        [deviceVc setSkipOrNextStepBlock:^(BOOL isNextStep) {
            if (isNextStep){
                
            }else {
                AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//                appdelegate.window.rootViewController = [[YFMMDrawerStateManager sharedManager] FetchDrawerController];
                appdelegate.window.rootViewController = [JCTabBarViewController new];
            }
        }];
        _controllerCache = @[uploadIconVc,personVc,racquetVc,deviceVc];
    }
    return _controllerCache;
}

- (void)nextOrPrevious:(BOOL)next {
    NSInteger index = self.collectionView.contentOffset.x / self.collectionView.bounds.size.width;
    if (next){
        // 进到下一页
        if (index < self.childViewControllers.count - 1) {
            index++;
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
        }
    }else {
        // 返回上一页
        if (index > 0) {
            index--;
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        }
    }
    if (index == 0) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else {
        if (index == 1) {
            self.title = @"Personal Data";
        }else if (index == 2){
            self.title = @"Racquet";
        }else if (index == 3){
            self.title = @"Connection devices";
        }
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
}

@end
